/**
 *
 */
package com.naiterui.ehp.bs.doctor.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.biz.TitleUtil;
import com.naiterui.ehp.bp.utils.esign.ESignUtils;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.doctor.config.TreatmentPresetOptionsConfig;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.fegin.EsignFeginClient;
import com.naiterui.ehp.bs.doctor.service.IDepartmentService;
import com.naiterui.ehp.bs.doctor.service.IDoctorService;
import com.naiterui.ehp.bs.doctor.utils.LimitValueConfig;
import com.naiterui.ehp.bs.doctor.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.doctor.vo.DepartmentAndTitleVO;
import com.naiterui.ehp.bs.doctor.vo.DoctorTitleVO;
import com.naiterui.ehp.bs.doctor.vo.ESignConfigVO;
import com.naiterui.ehp.bs.doctor.vo.GloableConfigVo;
import com.naiterui.ehp.bs.doctor.vo.LimitValueVO;
import com.naiterui.ehp.bs.doctor.vo.RecordPastVO;
import com.naiterui.ehp.bs.doctor.vo.TencentCloudConfigVO;
import com.naiterui.ehp.bs.doctor.vo.YwxAppConfigVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description 医生相关信息：如科室、职称 、出诊时间等
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.BASIC_INFO})
@RequestMapping("ad/doctorInfo")
@Controller
public class AdDoctorInfoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorInfoController.class);

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private EsignFeginClient esignFeginClient;

    @Autowired
    private TreatmentPresetOptionsConfig optionsConfig;

    /**
     * 获取科室和职称
     * @return
     */
    @ApiOperation(value = "获取科室和职称", notes = "获取科室和职称", httpMethod = "POST")
    @RequestMapping("departmentAndTitle")
    @ResponseBody
    public ResponseVO<DepartmentAndTitleVO> getDepartmentAndTitle() throws BusinessException {
        DepartmentAndTitleVO departmentAndTitleVO = new DepartmentAndTitleVO();
        try {
            departmentAndTitleVO.setDepartmentList(departmentService.getCascadeDedepartmentList());
            // ====职称
            List<DoctorTitleVO> titleList = new ArrayList<>();
            DoctorTitleVO doctorTitleVO = null;
            TitleUtil.Enum[] teArr = TitleUtil.Enum.values();
            for (TitleUtil.Enum te : teArr) {
                if (te.getId().equals(TitleUtil.Enum.MAIN_HOSPITAL_PHYSICIAN.getId())) {
                    // 给前端返回职称过滤掉医师和住院医师
                    continue;
                }
                doctorTitleVO = new DoctorTitleVO();
                doctorTitleVO.setTitleId(te.getId());
                doctorTitleVO.setTitleName(te.getName());
                titleList.add(doctorTitleVO);
            }
            departmentAndTitleVO.setTitleList(titleList);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("获取职称科室异常");
            throw new BusinessException(ExceptionCodes.FAILED);
        }
        return PublicService.returnResponseVO(departmentAndTitleVO);
    }


    /**
     * V2.5.0 新版蹦首页全局配置参数获取接口
     * DoctorInfoController.globalConfig()
     * @Date 2016年6月22日
     * @since 2.5.0 [2.16新增首页宣教未读标识]
     * @return
     */
    @ApiOperation(value = "取首页全局配置参数", notes = "获取首页全局配置参数", httpMethod = "POST")
    @RequestMapping("globalConfig")
    @ResponseBody
    public ResponseVO<GloableConfigVo> globalConfig(Long doctorId) throws BusinessException {
        GloableConfigVo globleConfigVO = new GloableConfigVo();
        // 初化药品用法用量列表
        globleConfigVO.initMedicationDefault();
        // 登陆失败联系客服电话
        globleConfigVO.setCustomerServPhone(ConfigUtil.getString("init.server.phone"));
        // 服药时间
        globleConfigVO.setMedicineEatTime(ConfigUtil.getString("init.server.medicine.eatTime"));
        // 购药单位
        globleConfigVO.setMedicineUnit(ConfigUtil.getString("init.medicine.unit"));
        // 药品用法
        globleConfigVO.setMedicineDirection(ConfigUtil.getString("init.medicine.direction"));
        // 医生勿扰提示语
        List<String> contentList = new ArrayList<>();
        contentList.add(ConfigUtil.getString("distrub.content1"));
        contentList.add(ConfigUtil.getString("distrub.content2"));
        contentList.add(ConfigUtil.getString("distrub.content3"));
        contentList.add(ConfigUtil.getString("distrub.content4"));
        contentList.add(ConfigUtil.getString("distrub.content5"));
        globleConfigVO.setDistrubList(contentList);
        // app分享主题
        globleConfigVO.setAppSpreadTitle(ConfigUtil.getString("app.spread.title"));
        // app分享医生内容
        globleConfigVO.setAppSpreadDrContent(ConfigUtil.getString("app.spread.dr.content"));
        // app分享患者内容
        globleConfigVO.setAppSpreadPtContent(ConfigUtil.getString("app.spread.pt.content"));
        // 患者账号禁用内容
        globleConfigVO.setPtDisabledContent(ConfigUtil.getString("dr.patient.accountdisabled.content"));
        // 互联网医院客服服务时间
        globleConfigVO.setServerTime(ConfigUtil.getString("pa.wroktime"));

        // 图文咨询默认价格列表(单位分)
        String chargeListStr = ConfigUtil.getString("consult.charge.list");
        List<Integer> chargeList = StringUtil.convertIntegerStr2List(chargeListStr, ",");
        globleConfigVO.setConsultChargeList(chargeList);

        // 极限值设
        LimitValueVO limitValueVO = LimitValueConfig.getLimitConfig();

        globleConfigVO.setLimitValue(limitValueVO);
        String redMsg = ConfigUtil.getString(PropertyValueConstants.BATCH_MESSAGE_RED_MSG);
        String greenMsg = ConfigUtil.getString(PropertyValueConstants.BATCH_MESSAGE_GREEN_MSG);
        String defaultMsg = ConfigUtil.getString(PropertyValueConstants.BATCH_MESSAGE_DEFAULT_MSG);
        globleConfigVO.setRedMsg(redMsg);
        globleConfigVO.setGreenMsg(greenMsg);
        globleConfigVO.setDefaultMsg(defaultMsg);

        // 设置医生填写病历页常用输入、药品过敏、家庭史输入项

        List<RecordPastVO> pastList = new ArrayList<>();
        RecordPastVO pastCommon = new RecordPastVO();
        pastCommon.setName(ConfigUtil.getString("past.common"));
        String pastCommonStr = ConfigUtil.getString("past.common.list");
        pastCommon.setItemList(Arrays.asList(pastCommonStr.split(",")));
        pastList.add(pastCommon);

        RecordPastVO pastDrug = new RecordPastVO();
        pastDrug.setName(ConfigUtil.getString("past.drug"));
        String pastDrugStr = ConfigUtil.getString("past.drug.list");
        pastDrug.setItemList(Arrays.asList(pastDrugStr.split(",")));
        pastList.add(pastDrug);

        RecordPastVO pastFamily = new RecordPastVO();
        pastFamily.setName(ConfigUtil.getString("past.family"));
        String pastFamilyStr = ConfigUtil.getString("past.family.list");
        pastFamily.setItemList(Arrays.asList(pastFamilyStr.split(",")));
        pastList.add(pastFamily);

        globleConfigVO.setPastList(pastList);

        //签名配置
        ESignConfigVO eSignConfigVO = new ESignConfigVO();
        eSignConfigVO.setSeviceName(ESignUtils.getServiceId());
        eSignConfigVO.setSealType(ESignUtils.getSealType());
        globleConfigVO.seteSignConfigVO(eSignConfigVO);

        //医院名称配置
        globleConfigVO.setHospitalName(ConfigUtil.getString("ehp.hospital.name"));
        // 线下实体医疗机构名
        globleConfigVO.setHospitalOrgName(ConfigUtil.getString("ehp.hospital.org.name"));

        //医网信签名账号配置
        YwxAppConfigVO ywxAppConfig = new YwxAppConfigVO();
        Map<String, String> signMap = esignFeginClient.getYwxSignConfig();
        ywxAppConfig.setClientId(signMap.get("clientId"));
        ywxAppConfig.setSecret(signMap.get("secret"));
        globleConfigVO.setBjcaSignConfig(ywxAppConfig);

        //腾讯云人脸识别账号配置
        TencentCloudConfigVO tencentCloudConfig = new TencentCloudConfigVO();
        tencentCloudConfig.setAppId(ConfigUtil.getString("tencent-cloud.ad.wbAppId"));
        tencentCloudConfig.setLicence(ConfigUtil.getString("tencent-cloud.ad.wbLicence"));
        globleConfigVO.setTencentCloudConfig(tencentCloudConfig);

        // 月经相关
        String menstrualFirstAgeStr = ConfigUtil.getString("menstrual.first.age.list");
        List<Integer> menstrualFirstAgeList = StringUtil.convertIntegerStr2List(menstrualFirstAgeStr, ",");
        globleConfigVO.setMenstrualFirstAgeList(menstrualFirstAgeList);
        String menstrualCycleStr = ConfigUtil.getString("menstrual.cycle.list");
        List<Integer> menstrualCycleList = StringUtil.convertIntegerStr2List(menstrualCycleStr, ",");
        globleConfigVO.setMenstrualCycleList(menstrualCycleList);
        String menstrualProcessDayStr = ConfigUtil.getString("menstrual.process.day.list");
        List<Integer> menstrualProcessDayList = StringUtil.convertIntegerStr2List(menstrualProcessDayStr, ",");
        globleConfigVO.setMenstrualProcessDayList(menstrualProcessDayList);

        // 家庭史
        String historyFamilyStr = ConfigUtil.getString("history.family.list");
        List<String> historyFamilyList = Arrays.asList(historyFamilyStr.split(","));
        globleConfigVO.setHistoryFamilyList(historyFamilyList);

        // 服用方式列表
        String[] split = ConfigUtil.getString("int.medicine.usageMethod").split(",");
        globleConfigVO.setUsageMethodList(Arrays.asList(split));

        // 治疗意见预制项
        List<String> options = optionsConfig.getOptions().stream().collect(Collectors.toList());
        globleConfigVO.setTreatmentPresetOptions(options);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, globleConfigVO);
    }

}
